package com.lyc.demo.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxySubjectImpl implements InvocationHandler {

    private Object proxy;

    public ProxySubjectImpl(Object object) {
        this.proxy = object;
    }

    public <T> T getProxy() {
        return (T) Proxy.newProxyInstance(proxy.getClass().getClassLoader(), proxy.getClass().getInterfaces(), this);
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("代理执行开始...");
        Object result = method.invoke(this.proxy, args);
        System.out.println("代理执行结束...");
        return result;
    }


    public static void main(String[] args) {
        Subject subject = new ProxySubjectImpl(new RealSubjectImpl()).getProxy();
        subject.execute("abc");
    }
}
